// pages/edit-data/edit-data.js
let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        myData: null,
        nickName: null,
        desc: null,
        baseImg: null
    },
    onShow() {
        wx.request({
            url: 'http://www.kangliuyong.com:10002/findAccountInfo',
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token
            },
            success: res => {
                console.log(res);
                this.setData({
                    myData: res.data.result[0],
                })
            }
        })
    },
    // 图片转base64
    uploadImg() {
        wx.chooseMedia({
            success: res => {
                console.log(res);
                wx.getFileSystemManager().readFile({
                    filePath: res.tempFiles[0].tempFilePath,
                    encoding: 'base64',
                    success: res => {
                        console.log(res);
                        this.setData({
                            baseImg: res.data,
                        })
                    }
                })
            }
        })
    },
    // // 保存资料
    saveData() {
        console.log(this.data.baseImg);
        let nickName = this.data.nickName
        let desc = this.data.desc
        let myData = this.data.myData
        if (!this.data.myData.nickName) {
            wx.showToast({
                title: '昵称不能为空',
                icon: 'error'
            })
        }
        if (!desc) {
            this.setData({
                desc: myData.desc
            })
        }
        if (!nickName) {
            this.setData({
                nickName: myData.nickName,
            })
        }
        if (!nickName && !desc) {
            this.setData({
                nickName: myData.nickName,
                desc: myData.desc
            })
        }
        if (!this.data.baseImg) {
            this.setData({
                baseImg: myData.userImg
            })
        } else {
            // 修改昵称
            wx.request({
                url: 'http://www.kangliuyong.com:10002/updateNickName',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: app.globalData.token,
                    nickName: this.data.nickName
                },
                success: res => {
                    console.log(res);
                }
            })
            // 修改简介
            wx.request({
                url: 'http://www.kangliuyong.com:10002/updateDesc',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: app.globalData.token,
                    desc: this.data.desc
                },
                success: res => {
                    console.log(res);
                }
            })
            // 上传头像
            wx.request({
                url: 'http://www.kangliuyong.com:10002/updateAvatar',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: app.globalData.token,
                    imgType: ['jpg', 'jpeg', 'png'],
                    serverBase64Img: this.data.baseImg
                },
                success: res => {
                    console.log(res);
                }
            })
            wx.showToast({
                title: '修改成功',
            })
        }
        console.log(this.data.desc);

    }
})